# -*- coding: utf-8 -*-
# @Time    : 2024/3/16 16:45
# @Author  : floraachy
# @File    : repo_code_page
# @Software: PyCharm
# @Desc:

# 标准库导入
# 第三方库导入
import allure
# 本地应用/模块导入
from utils.base_utils.base_page import BasePage


class RepoCodePage(BasePage):
    """项目-代码库页面"""
    # 导航栏
    locator_repo_nav = "xpath=//span[contains(text(), '{}')]"
    # '文件'按钮
    locator_file_btn = "//a[text()='文件 ']"
    # 新建文件按钮
    locator_new_file_btn = "//li[@role='menuitem']/a[text()='新建文件']"
    # 上传文件按钮
    locator_upload_file_btn = "//li[@role='menuitem']/a[text()='上传文件']"
    # 复刻按钮
    locator_fork_btn = "//span[text()='复刻(Fork)']"
    # 代码库所有文件的文件名
    locator_filename_all_a = "//ul[@class='listtablebody']//i/parent::a"
    # 代码库 某个文件名
    locator_filename_a = "//ul[@class='listtablebody']//i/parent::a[text()='{}']"
    # 项目私有标签
    locator_private_label = "xpath=//span[text()='私有']"
    # 复刻旁边的数字
    locator_fork_number = "//span[text()='复刻(Fork)']/../following-sibling::a[@class='detail_tag_btn_count']"
    # 代码库选中的分支名
    locator_repo_selected_branch = "//span[@class='ant-dropdown-link task-hide']"
    # 合并请求按钮
    locator_pr_btn = "//a[text()='+ 合并请求']"
    # 分支下拉框
    locator_branch_dropdown = "//div[@class='branch-tagBox ant-dropdown-trigger']"
    # 分支名
    locator_branch_name = "//a[text()='{}']"
    # 最新一次的提交记录
    locator_newest_commit = "//pre[@id='ptxt']//p"

    @allure.step("访问代码库页面：/{repo_owner}/{repo_identifier}")
    def navigate(self, repo_owner, repo_identifier):
        """
        访问代码库页面
        """
        self.visit(f"/{repo_owner}/{repo_identifier}")

    @allure.step("点击项目详情页的导航栏：【{nav}】")
    def click_repo_nav_button(self, nav):
        """
        点击项目详情页的导航栏
        """
        self.wait(1)
        self.click(self.locator_repo_nav.format(nav))

    @allure.step("点击【文件】按钮")
    def click_file_button(self):
        """
        点击'文件'按钮，显示新建文件，上传文件
        """
        self.click(locator=self.locator_file_btn)
        self.page.wait_for_load_state()

    @allure.step("点击【新建文件】按钮")
    def click_create_file_button(self):
        """
       点击'新建文件'按钮
       """
        self.click(locator=self.locator_new_file_btn)
        self.page.wait_for_load_state()

    @allure.step("点击【上传文件】按钮")
    def click_upload_file_button(self):
        """
       点击'上传文件'按钮
       """
        self.click(locator=self.locator_upload_file_btn)

    @allure.step("获取代码库所有的文件名（一级）")
    def get_repo_all_filename(self):
        """
        获取代码库所有的文件名（一级）
        """
        return self.get_all_elements_text(locator=self.locator_filename_all_a)

    @allure.step("点击文件：{filename}")
    def click_repo_file(self, filename):
        """
        点击代码库某个文件
        """
        self.click(locator=self.locator_filename_a.format(filename))

    @allure.step("代码库页面，点击最新一次的提交记录")
    def click_newest_commit(self):
        """
        代码库页面，点击最新一次的提交记录
        """
        self.click(self.locator_newest_commit)

    @allure.step("切换到分支：{branch_name}")
    def switch_branch(self, branch_name):
        """
        切换到制定分支
        """
        self.click(self.locator_branch_dropdown)
        self.click(self.locator_branch_name.format(branch_name))

    @allure.step("获取当前选中分支名称")
    def get_current_branch_name(self):
        """
        获取当前选中分支名称
        """
        return self.get_text(self.locator_repo_selected_branch)

    @allure.step("代码库页面，点击【+合并请求】按钮")
    def click_add_pull_request_button(self):
        self.click(self.locator_pr_btn)

    @allure.step("点击【复刻(Fork)】按钮")
    def click_fork_button(self):
        self.click(self.locator_fork_btn)

    @allure.step("点击仓库fork按钮旁边的数字")
    def click_fork_number(self):
        """
        点击仓库fork按钮旁边的数字，进入仓库fork列表
        """
        self.click(self.locator_fork_number)
